package com.ybbase.framework.config.swagger2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cg
 * @Des Swagger2配置类
 * @Date 2019-07-04
 */

@Configuration
@EnableSwagger2
public class Swagger2 {

    /**
     * 流程管理
     *
     * @return
     * @author cg
     * @date 2019-08-27 17:34
     */
    @Bean
    public Docket activitiRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.ACTIVITI_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.ACTIVITI_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 产品包
     *
     * @return
     * @author cg
     * @date 2019-08-27 17:34
     */
    @Bean
    public Docket coalRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.COAL_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.COAL_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 合同包
     *
     * @return
     * @author cg
     * @date 2019-08-27 17:34
     */
    @Bean
    public Docket contractRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.CONTRACT_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.CONTRACT_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 客户包
     *
     * @return
     * @author cg
     * @date 2019-08-27 17:34
     */
    @Bean
    public Docket customerRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.CUSTOMER_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.CUSTOMER_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 公路运输包
     *
     * @return
     * @author cg
     * @date 2019-08-27 17:34
     */
    @Bean
    public Docket highwayRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.HIGHWAY_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.HIGHWAY_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 销售包
     *
     * @return
     * @author cg
     * @date 2019-08-27 17:34
     */
    @Bean
    public Docket saleRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.SALE_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.SALE_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 系统管理
     *
     * @return
     * @author cg
     * @date 2019-08-27 17:34
     */
    @Bean
    public Docket systemRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.SYSTEM_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.SYSTEM_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 提煤单包
     *
     * @return
     * @author cg
     * @date 2019-08-27 17:34
     */
    @Bean
    public Docket voucherRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.VOUCHER_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.VOUCHER_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * @return
     * @author yanBin
     * @date 2019/9/10 10:49
     * @since 4.0
     */
    @Bean
    public Docket qualityRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.QUALITY_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.QUALITY_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 配置公共header
     *
     * @param
     * @return
     * @throws
     * @author cg
     * @date 2019-08-27 17:28
     * @since
     */
    public List<Parameter> operationParameters() {
        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("x_auth_token")
                .description("JWT令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(tokenPar.build());
        //添加head参数end
        return pars;
    }

    /**
     * 配置公共信息
     *
     * @param
     * @return
     * @throws
     * @author cg
     * @date 2019-08-27 17:29
     * @since
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Tsmis 开放后台RestApi接口")
                .description("更多详细请联系开发者")
                .termsOfServiceUrl("http://www.metchain.com/")
                .contact(new Contact("MET", "", ""))
                .version("1.0")
                .build();
    }

    /**
     * 产量计划
     *
     * @return Docket
     * @author xuwenwu
     * @date 2019-09-17 17:26
     * @since 4.0
     */
    @Bean
    public Docket planOutputApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.PLAN_OUTPUT_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.PLAN_OUTPUT_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 客户账款管理
     *
     * @param
     * @return
     * @author yanBin
     * @date 2019/9/23 10:02
     * @since 4.0
     */
    @Bean
    public Docket custAcctApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.CUST_ACCT_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.CUST_ACCT_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 过磅管理模块
     *
     * @return Docket
     * @author yanBin
     * @date 2019/10/18 11:52
     * @since 4.0
     */
    @Bean
    public Docket weighingApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.WEIGHING_PACKAGE_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.WEIGHING_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 煤矿管理
     *
     * @return Docket
     * @author yanBin
     * @date 2019/10/21 15:29
     * @since 4.0
     */
    @Bean
    public Docket collieryApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.COLLIRERY_PACKAGE_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.COLLIRERY_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 基础数据
     *
     * @return
     * @author shenaiguo
     * @date 2019-10-30 10:34
     */
    @Bean
    public Docket paramRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.PARAM_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.PARAM_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }


    /**
     * 铁路运输
     *
     * @author baijingbin
     * @date 2019-10-30 10:34
     */
    @Bean
    public Docket railwayRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(operationParameters())
                .groupName(RestApiConstant.RAILWAY_GROUP_NAME)
                .genericModelSubstitutes(DeferredResult.class)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(RestApiConstant.RAILWAY_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }
}
